// pages/overview/overview.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        curYear: new Date().getFullYear(), // 年份
        curMonth: new Date().getMonth() + 1,// 月份 1-12
        day: new Date().getDate(), // 日期 1-31 若日期超过该月天数，月份自动增加

        selectDate:'',
        doneList:[],
        undoneList:[],
        listDisplay:true,
        todayTaskNum:-1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    intoToDo:function (e) {
        wx.navigateTo({
          url: '/pages/todoDetail/todoDetail?detail='+JSON.stringify(e.currentTarget.dataset.detail),
        })
    },

    addTodo:function () {
        wx.navigateTo({
          url: '/pages/addTodo/addTodo',
        })
    },

    prevMonth:function () {
      this.setData({
          listDisplay:false
      })  
    },

    nextMonth:function () {
        this.setData({
            listDisplay:false
        })  
      },

    timeToString:function () {
        let year=this.data.curYear.toString()
        let month=this.data.curMonth<10?'0'+this.data.curMonth:this.data.curMonth.toString()
        let day=this.data.day<10?'0'+this.data.day:this.data.day.toString()
        let time=year+'-'+month+'-'+day
        return time
    },

    selectDate:function (e) {
        let stamp=Number(new Date(e.detail.date))
        var that=this
        that.setData({
            selectDate:e.detail.date,
            listDisplay:true
        })
        that.getList(stamp)
    },

    getList:function (stamp) {
        var that=this
        wx.cloud.callFunction({
            name: 'getToDoAtFixedTime',
            data:{
              time:stamp,
              done:true
           },
            success:function (res) {
            that.setData({
              doneList:res.result.data,
            })
            }
        })
        wx.cloud.callFunction({
            name: 'getToDoAtFixedTime',
            data:{
              time:stamp,
              done:false
           },
            success:function (res) {
                if(that.timeToString()==that.data.selectDate){
                    that.setData({
                        undoneList:res.result.data,
                        todayTaskNum:res.result.data.length
                    })
                }
                else{
                    that.setData({
                        undoneList:res.result.data
                    })
                }
            

            }
        })
    },

    setDone:function(e){
        var that=this
        wx.cloud.callFunction({
          name: 'updateDone',
          data:{
            id:e.currentTarget.dataset.undoneset._id,
            done:true
          },
          success:function (res) {
            that.onShow()
          }
          })
      },
    
    setUndone:function(e){
        var that=this
        wx.cloud.callFunction({
            name: 'updateDone',
            data:{
            id:e.currentTarget.dataset.doneset._id,
            done:false
            },
            success:function (res) {
                that.onShow()
            }
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if(this.data.selectDate.length==0)
        {
            //初始情况，未选择日期
            let time=this.timeToString()
            this.setData({
                selectDate:time,
            })
            let stamp=Number(new Date(time))
            console.log(stamp)
            this.getList(stamp)
            //console.log("initial")
        }
        else{
            let stamp=Number(new Date(this.data.selectDate))
            this.getList(stamp)
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.onShow()
        wx.stopPullDownRefresh({
        success: (res) => {},
        })
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return{
            title:'',
            path:'/pages/index/index',
            imageUrl:'/images/icon.png',
            success:(res)=>{
              
            }
        }
    }
})
